package com.tao.utilslib.list;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by Tao on 2018/11/27.
 */

public class MapUtil {

    public interface ICondition<K, V> {
        boolean satisy(K next, V value);
    }

    public static <K, V> void removeMapItems(HashMap<K, V> hashMap, ICondition<K,V> condition) {
        Iterator<K> iterator = hashMap.keySet().iterator();
        while (iterator.hasNext()) {
            K next = iterator.next();
            V value = hashMap.get(next);
            if (condition.satisy(next, value)) {
                iterator.remove();
            }
        }

    }

    public enum IteratorType {
        next, ret
    }

    public interface IteratorCall<K, D> {
        IteratorType onIterator(K key, D value);
    }

    public static <T> T iteratorMap(Map map, IteratorCall iteratorCall) {
        if (map == null)
            return null;
        Iterator iterator = map.keySet().iterator();
        while (iterator.hasNext()) {
            Object next = iterator.next();
            Object o = map.get(next);
            switch (iteratorCall.onIterator(next, o)) {
                case ret:
                    return (T) o;
                case next:
                    continue;
            }
        }
        return null;
    }

}
